<?php
/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */
namespace Admin\Controller;
use Think\AjaxPage;
/**
 * 分销应用
 */
class AppDistributController extends BaseController {
    /**
     * 分销关系
     */
    public function index() {
        $user_id = I('post.user_id');
        if (!empty($user_id)) {
            $rs = D('users')->where(array('is_distribut' => 1, 'first_leader' => array('eq', 0), 'user_id' => $user_id))->field('user_id,nickname')->select();
        } else {
            $rs = D('users')->where(array('is_distribut' => 1, 'first_leader' => array('eq', 0)))->field('user_id,nickname')->limit(10)->select();
        }
        foreach ($rs as $k => $v) {
            $rs1 = D('users')->where(array('first_leader' => $v['user_id']))->getField('user_id,user_id,nickname');
            if (count($rs1)) {
                $rs[$k]['first'] = $rs1;
                foreach ($rs1 as $k1 => $v1) {
                    $rs2 = D('users')->where(array('first_leader' => $k1))->getField('user_id,user_id,nickname');
                    if (count($rs2)) {
                        $rs[$k]['first'][$k1]['first'] = $rs2;
                        foreach ($rs2 as $k2 => $v2) {
                            $rs3 = D('users')->where(array('first_leader' => $k2))->getField('user_id,user_id,nickname');
                            $rs[$k]['first'][$k1]['first'][$k2]['first'] = $rs3;
                        }
                    }
                }
            }
        }
        $this->assign('distributRs', $rs);
        $this->assign('user_id', $user_id);
        $this->display();
    }
    /**
     * 分销设置
     */
    public function set() {
        $inc_type = 'distribut';
        $this->assign('inc_type', $inc_type);
        $this->assign('config', xrkCache($inc_type));
        if (IS_POST) {
            $param = I('post.');
            $inc_type = $param['inc_type'];
            unset($param['inc_type']);
            xrkCache($inc_type, $param);
            $this->success("操作成功");
            exit();
        }
        $this->display();
    }
    /**
     * 提现列表
     */
    public function cash() {
        $this->display();
    }
    public function ajaxCash() {
        $condition = array();
        if (IS_POST) {
            if (I('status') != '-1') {
                $condition['status'] = I('status');
            }
            if (I('user_id')) {
                $condition['user_id'] = I('user_id');
            }
            if (I('account_bank')) {
                $condition['account_bank'] = I('account_bank');
            }
            if (I('account_name')) {
                $condition['account_name'] = I('account_name');
            }
        }
        $model = D('cash');
        $count = $model->where($condition)->count();
        $Page = new AjaxPage($count, 10);
        foreach ($condition as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $cashList = $model->where($condition)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        $show = $Page->show();
        $this->assign('cashList', $cashList);
        $this->assign('page', $show);
        $this->display();
    }
    /**
     * 删除提现记录
     */
    public function delCash() {
        $id = I('id');
        if (empty($id)) {
            $this->error('参数错误!');
        }
        D('cash')->where(array('id' => $id))->delete();
        $this->success('成功删除!');
    }
    /**
     * 审核提现
     */
    public function auditCash() {
        $id = I('id');
        if (empty($id)) {
            $this->error('参数错误!');
        }
        $cash = D('cash')->where(array('id' => $id))->find();
        $user = D('users')->where(array('user_id' => $cash['user_id']))->field('user_id,user_money,nickname')->select();
        if (IS_POST) {
            $data['status'] = I('post.status');
            $data['remark'] = I('post.remark');
            D('cash')->where(array('id' => $id))->save($data);
            ////
            if (I('post.status') == 1) {
                $user_money = floatval($user[0]['user_money']) - floatval($cash['money']);
                D('users')->where(array('user_id' => $cash['user_id']))->setField('user_money', $user_money);
                ////
                $user_log_data['user_id'] = $cash['user_id'];
                $user_log_data['user_money'] = -$cash['money'];
                $user_log_data['change_time'] = time();
                $user_log_data['desc'] = '平台提现';
                D('user_log')->add($user_log_data);
            }
            $this->success('操作成功!', U('Admin/AppDistribut/cash'));
            exit();
        }
        $this->assign('cash', $cash);
        $this->assign('user', $user[0]);
        $this->display();
    }
    /**
     * 汇款列表
     */
    public function record() {
        $this->display();
    }
    public function ajaxRecord() {
        $condition = array();
        if (IS_POST) {
            if (I('user_id')) {
                $condition['user_id'] = I('user_id');
            }
            if (I('account_bank')) {
                $condition['account_bank'] = I('account_bank');
            }
            if (I('account_name')) {
                $condition['account_name'] = I('account_name');
            }
        }
        $condition['status'] = 1;
        $model = D('cash');
        $count = $model->where($condition)->count();
        $Page = new AjaxPage($count, 10);
        foreach ($condition as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $cashList = $model->where($condition)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        $show = $Page->show();
        $this->assign('cashList', $cashList);
        $this->assign('page', $show);
        $this->display();
    }
    /**
     * 分成列表
     */
    public function bonus() {
        $this->display();
    }
    public function ajaxBonus() {
        $condition = array();
        if (IS_POST) {
            if (I('status') != '-1') {
                $condition['status'] = I('status');
            }
            if (I('user_id')) {
                $condition['user_id'] = I('user_id');
            }
            if (I('order_sn')) {
                $condition['order_sn'] = I('order_sn');
            }
        }
        $model = D('bonus_log');
        $count = $model->where($condition)->count();
        $Page = new AjaxPage($count, 10);
        foreach ($condition as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $bonusList = $model->where($condition)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        $show = $Page->show();
        $this->assign('bonusList', $bonusList);
        $this->assign('page', $show);
        $this->display();
    }
    /**
     * 修改分成备注
     */
    public function editBonus() {
        $id = I('id');
        if (empty($id)) {
            $this->error('参数错误!');
        }
        $bonus = D('bonus_log')->where(array('id' => $id))->find();
        if (IS_POST) {
            $data['remark'] = I('post.remark');
            D('bonus_log')->where(array('id' => $id))->save($data);
            $this->success('操作成功!', U('Admin/AppDistribut/bonus'));
            exit();
        }
        $this->assign('bonusLog', $bonus);
        $this->display();
    }
}
